package com.jvm;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 项目名称: java_algorithm
 * @ClassName CyclicBarrierTest
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2021/10/14 9:15
 */
public class CyclicBarrierTest {

    /**
     * CyclicBarrier构造函数的参数数量表示有几个线程执行了await方法才 合并放行
     *
     */
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        CyclicBarrier cyclicBarrier=new CyclicBarrier(3, () -> System.out.println("ffffff"));
        new Thread(()->{
            System.out.println("线程1开始");
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println("线程1结束");
        }).start();

        new Thread(()->{
            try {
                Thread.sleep(2000);
                System.out.println("线程2开始");
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println("线程2结束");
        }).start();

        Thread.sleep(5000);
        cyclicBarrier.await();
        System.out.println("ggggggggg");
    }

}
